﻿
using DABLL;
using Microsoft.IdentityModel.Tokens;
using SqlSugar.Extensions;

namespace DataAcquisition
{
    public partial class SetForm : BaseForm
    {
        public SetForm()
        {
            InitializeComponent();
            Header.Text = "系统设置";
        }

        private void SetForm_Load(object sender, EventArgs e)
        {
            PortSelect.Text = Config.PortAddress;
            BaudrateSelect.Text = Config.Baudrate.ToString();
            RunBroadCheck.Checked = Config.RunBroad;
            RunInput.Text = Config.RunBroadMessage;
            StartBroadCheck.Checked = Config.StartBroad;
            StartInput.Text = Config.StartBroadMessage;
            ErrorBroadCheck.Checked = Config.ErrorBroad;
            ErrorIntervalInput.Text = Config.ErrorBroadInterval.ToString();
            BroadStageCheck.Checked = Config.StageBroad;
            TestPointCountInput.Text = Config.PointCount.ToString();
        }

        /// <summary>
        /// 保存语音设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveVoiceButton_Click(object sender, EventArgs e)
        {
            if (PortSelect.Text.IsNullOrEmpty())
            {
                Error("请选择串口地址");
                return;
            }
            Config.PortAddress = PortSelect.Text;

            if (BaudrateSelect.Text.IsNullOrEmpty())
            {
                Error("请选择波特率");
                return;
            }
            Config.Baudrate = BaudrateSelect.Text.ObjToInt();
            Success("已保存");
        }

        private void SaveBroadButton_Click(object sender, EventArgs e)
        {
            if (RunBroadCheck.Checked && RunInput.Text.IsNullOrEmpty())
            {
                Error("请输入播报内容");
                RunInput.Focus();
                return;
            }
            Config.RunBroad = RunBroadCheck.Checked;
            Config.RunBroadMessage = RunInput.Text;

            if (StartBroadCheck.Checked && StartInput.Text.IsNullOrEmpty())
            {
                Error("请输入播报内容");
                StartInput.Focus();
                return;
            }
            Config.StartBroad = StartBroadCheck.Checked;
            Config.StartBroadMessage = StartInput.Text;

            Config.ErrorBroad = ErrorBroadCheck.Checked;
            Config.ErrorBroadInterval = ErrorIntervalInput.Text.ObjToInt();

            Config.StageBroad = BroadStageCheck.Checked;
            Success("已保存");
        }

        private void DeviceButton_Click(object sender, EventArgs e)
        {
            DeviceManageForm frm = new DeviceManageForm();
            frm.ShowDialog();
        }

        private void ProjectButton_Click(object sender, EventArgs e)
        {
            ProjectManageForm frm = new ProjectManageForm();
            frm.ShowDialog();
        }

        private void TemplateButton_Click(object sender, EventArgs e)
        {
            TemplateManageForm frm = new TemplateManageForm();
            frm.ShowDialog();
        }

        private void FormPointButton_Click(object sender, EventArgs e)
        {
            FormPointManageForm frm = new FormPointManageForm();
            frm.ShowDialog();
        }

        private void TestButton_Click(object sender, EventArgs e)
        {
           int count = TestPointCountInput.Text.ObjToInt();
            if (count < 5 || count > 20)
            {
                Error("点位数量超出范围");
                return;
            }
            Config.PointCount = count;
            Success("已保存");
        }
    }
}
